import {userMgr} from "../modules/userModule/managers/UserManager";
import {showLoading} from "../modules/coreModule/managers/LoadingManager";
import {handleError} from "../modules/coreModule/managers/AlertManager";
import {CoreLoading} from "../modules/coreModule/CoreCommon";

export type MediaFile = {
	size: number
	thumb: string
	type: "image" | "video"
	url: string
}

export type File = {
	name: string,
	url: string,
	type: string
}

export class CloudFileUtils {

	/**
	 * 媒体文件转化为文件
	 */
	public static mediaFile2File(name: string, file: MediaFile): File {
		const ext = this.getExt(file.url);
		return {
			name: this.generateFileName(name, ext),
			url: file.url, type: file.type
		}
	}

	/**
	 * 生成文件名
	 */
	public static generateFileName(name, ext) {
		const openid = userMgr().openid;
		return `${openid}-${Date.now()}-${Math.round(Math.random()*10000)}-${name}.${ext}`;
	}

	/**
	 * 上传多个图片至云存储，并返回永久fileID
	 * @param {Array} files 文件列表
	 * @param {String} path 指定储存的文件夹路径
	 */
  // @handleError(true)
  @showLoading(CoreLoading.Upload)
  public static async uploadFiles(files: File[], path: string){
    console.log("uploadFiles: ", files, path);
    const tasks: any = files.map(f => this.uploadFile(f, path));
    // tasks是若干Promise对象的集合，此时已经上传成功
    // 用下面的方法，data就是提取出来的返回结果
    const data:any = await Promise.all(tasks);/*  */
    // 把固定地址返回，保存进数据库
		console.log(data);
		const res = data.map(f => f.fileID);
    return res;
  }


	/**
	 * 上传单个图片
	 * @param file
	 * @param path
	 */
  public static uploadFile(file, path):Promise<any> {
    console.log(file, path);
    return new Promise((resolve, reject) => {
      wx.cloud.uploadFile({
        cloudPath: `${path}/${file.name}`,
        filePath: file.url,
        success: res => {
          console.log('上传成功');
          resolve(res);
        },
        fail: res => {
          console.log('上传失败');
          reject(res);
        }
      })
    })
  }

	/**
	 * 获取拓展名
	 * @param path
	 */
	public static getExt(path) {
		return path.substr(path.lastIndexOf(".") + 1);
	}
}
